import React, { Component } from 'react';
import { addGun } from './index.redux.js' 

class App extends Component {
    constructor(props) {
      super(props)
      this.store = this.props.store
      this.state = {
        num: this.store.getState()
      }
      this.listener = this.listener.bind(this)
      this.store.subscribe(this.listener)
    }
    listener() {
      //store发生变化，组件更新状态
      //调用setSate主要目的是为了重新渲染render
      this.setState({
        num: this.store.getState()
      })
    }
    render() {
      return (
        <div>
          <p>现在有武器{this.state.num}</p>
          <button onClick={() => this.store.dispatch(addGun())}>申请武器</button>
        </div>
      )
    }
}

export default App;
